﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;
using Nardax.Serialization;

namespace Nardax.Azure.StorageClient
{
    public class XmlCloudQueueWrapper<T> : CloudQueueWrapperBase, ICloudQueue<T>
    {
        private readonly ISerializer<T, string> _serializer;

        public XmlCloudQueueWrapper(CloudQueue cloudQueue, ISerializer<T, string> serializer)
            : base(cloudQueue)
        {
            _serializer = serializer;
        }

        public void AddMessage(T message)
        {
            var serializedValue = _serializer.Serialize(message);
            var cloudQueueMessage = new CloudQueueMessage(serializedValue);
            CloudQueue.AddMessage(cloudQueueMessage);
        }

        public void AddMessage(T message, TimeSpan timeToLive)
        {
            var serializedValue = _serializer.Serialize(message);
            var cloudQueueMessage = new CloudQueueMessage(serializedValue);
            CloudQueue.AddMessage(cloudQueueMessage, timeToLive);
        }

        public ICloudQueueMessage<T> GetMessage()
        {
            var cqm = CloudQueue.GetMessage();
            if (cqm != null)
            {
                var mv = _serializer.Deserialize(cqm.AsString);
                return new CloudQueueMessageWrapper<T>(cqm, mv);
            }

            return default(ICloudQueueMessage<T>);
        }

        public ICloudQueueMessage<T> GetMessage(TimeSpan visibilityTimeout)
        {
            var cqm = CloudQueue.GetMessage(visibilityTimeout);
            if (cqm != null)
            {
                var mv = _serializer.Deserialize(cqm.AsString);
                return new CloudQueueMessageWrapper<T>(cqm, mv);
            }

            return default(ICloudQueueMessage<T>);
        }

        public IEnumerable<ICloudQueueMessage<T>> GetMessages(int messageCount)
        {
            return (from cqm in CloudQueue.GetMessages(messageCount)
                    select new CloudQueueMessageWrapper<T>(cqm, _serializer.Deserialize(cqm.AsString))).ToList();
        }

        public IEnumerable<ICloudQueueMessage<T>> GetMessages(int messageCount, TimeSpan visibilityTimeout)
        {
            return (from cqm in CloudQueue.GetMessages(messageCount, visibilityTimeout)
                    select new CloudQueueMessageWrapper<T>(cqm, _serializer.Deserialize(cqm.AsString))).ToList();
        }

        public ICloudQueueMessage<T> PeakMessage()
        {
            var cqm = CloudQueue.PeekMessage();
            if (cqm != null)
            {
                var mv = _serializer.Deserialize(cqm.AsString);
                return new CloudQueueMessageWrapper<T>(cqm, mv);
            }

            return default(ICloudQueueMessage<T>);
        }

        public IEnumerable<ICloudQueueMessage<T>> PeakMessages(int messageCount)
        {
            return (from cqm in CloudQueue.PeekMessages(messageCount)
                    select new CloudQueueMessageWrapper<T>(cqm, _serializer.Deserialize(cqm.AsString))).ToList();
        }
    }
}